package com.zhou.test.spring.secrity.security;

import com.zhou.test.spring.secrity.entity.SysUser;
import com.zhou.test.spring.secrity.model.security.MyUserDetails;
import com.zhou.test.spring.secrity.service.impl.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * Security 加载用户信息服务
 * <p>
 * @serial 2.0.0
 * @author 就眠儀式
 */
@Slf4j
@Component
public class SecureUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.loadUserByUsername(username);
        //todo 查询角色，查询权限，查询组织
        MyUserDetails userDetails=new MyUserDetails(sysUser);
        return userDetails;
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        log.info(passwordEncoder.encode("123456"));
    }

}
